package cn.itsource.article.controller;


import cn.itsource.article.domain.Article;
import cn.itsource.article.query.ArticleQuery;
import cn.itsource.article.service.IArticleService;
import cn.itsource.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private IArticleService service;

    /**
     * 新增或者修改
     * @param user  如何user的id为空就是新增 否则就是修改
     * @return
     */
    @PostMapping
    public AjaxResult save(@RequestBody Article user){
        try {
            service.save(user);
            return AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


    /**
     * 文章类型分组
     * @return
     */

    @PatchMapping("/group")
    public AjaxResult group(){
        try {
            return AjaxResult.me().setResultObj(service.group()).setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 归档分组
     * @return
     */

    @PatchMapping("/createGroup")
    public AjaxResult createGroup(){
        try {
            return AjaxResult.me().setResultObj(service.createGroup()).setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 根据id进行删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.deleteByPrimaryKey(id);
            return AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     *  分页查询
     * @param userQuery
     * @return
     */
    @PatchMapping
    public AjaxResult pageInfo(@RequestBody ArticleQuery userQuery){
        try {
            return AjaxResult.me().setSuccess(true).setMessage("操作成功").setResultObj(service.pageInfo(userQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

    /**
     * 批量删除   由于批量删除 前端可以看到传过来的其实是一个一个对象 把这些对象装在一个集合里面接收
     * @param
     * @return
     */
    @PatchMapping("/patchDelete")
    public AjaxResult patchDel(@RequestBody List<Article> users){
        try {
            service.deleteByCollection(users);
            return  AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

    /**
     *
     * @param
     * @return
     */
    @GetMapping("/{articleId}")
    public AjaxResult get(@PathVariable("articleId")  Long articleId){
        try {
            return AjaxResult.me().setSuccess(true).setMessage("操作成功").setResultObj(service.selectByarticleId(articleId));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

    @PatchMapping("/aboutArticles/{articleId}")
    public AjaxResult aboutArticles(@PathVariable("articleId")  Long articleId){
        try {
            return AjaxResult.me().setResultObj(service.aboutArticle(articleId));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

    /**
     * 点赞数
     * @param articleId
     * @return
     */
   @PostMapping("/star/{articleId}")
    public AjaxResult star(@PathVariable("articleId")  String articleId){
        try {
            service.addNum(articleId);
            return AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 浏览数
     * @param articleId
     * @return
     */
    @PostMapping("/seeWarch/{articleId}")
    public AjaxResult seeWarchr(@PathVariable("articleId")  String articleId){
        try {
            service.addWatch(articleId);
            return AjaxResult.me().setSuccess(true).setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 竖型图展示
     * @param
     * @return
     */
    @GetMapping("/getList")
    public AjaxResult getList(){
        try {
             service.getList();
            return AjaxResult.me().setSuccess(true).setMessage("操作成功").setResultObj(service.getList());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }
}

